﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using FindPharmacies.Model;
using FindPharmacies.Models;

namespace FindPharmacies.Controllers
{
    public class PharmacyController : Controller
    {

        IPharmacyRepository _repository;
        public PharmacyController()
        {
            _repository = new SqlPharmacyRepository();
        }
        public PharmacyController(IPharmacyRepository repository)
        {
            _repository = repository;
        }
        //
        // GET: /Pharmacy/

        public ActionResult Index()
        {
            return View();
        }

        public ActionResult Heidelberg()
        {
            if (Request.IsAjaxRequest())
            {
                var pharmacies = _repository.FindAllPharmacies().Where(x => x.City == "Heidelberg").ToList();


                return Json(PharmacyViewModel.GetJsonPharmacies(pharmacies), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return View();
            }
        }

        public ActionResult Mannheim()
        {
            if (Request.IsAjaxRequest())
            {
                var pharmacies = _repository.FindAllPharmacies().Where(x => x.City == "Mannheim").ToList();


                return Json(PharmacyViewModel.GetJsonPharmacies(pharmacies), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return View();
            }
        }

        public ActionResult EmergencyHeidelberg()
        {
            if (Request.IsAjaxRequest())
            {
                var pharmacies = _repository.FindAllPharmacies().Where(x => x.EmergencyService == true && x.City == "Heidelberg").ToList();


                return Json(PharmacyViewModel.GetJsonPharmacies(pharmacies), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return View();
            }
        }

        public ActionResult EmergencyMannheim()
        {
            if (Request.IsAjaxRequest())
            {
                var pharmacies = _repository.FindAllPharmacies().Where(x => x.EmergencyService == true && x.City == "Mannheim").ToList();


                return Json(PharmacyViewModel.GetJsonPharmacies(pharmacies), JsonRequestBehavior.AllowGet);
            }
            else
            {
                return View();
            }
        }


        public ActionResult Admin()
        {
            var pharmacies = _repository.FindAllPharmacies();
            return View(pharmacies);
        }

        //
        // GET: /Pharmacy/Details/5

        public ActionResult Details(int id)
        {
            var pharmacy = _repository.GetPharmacy(id);
            return View(pharmacy);
        }

        //
        // GET: /Pharmacy/Create

        public ActionResult Create()
        {
            return View();
        } 

        //
        // POST: /Pharmacy/Create

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Pharmacy pharmacy)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    // TODO: Add insert logic here
                    _repository.Add(pharmacy);
                    _repository.Update(pharmacy);
                    return RedirectToAction("Admin");
                }
                catch
                {
                    return View(pharmacy);
                }
            }
            else
            {
                return View(pharmacy);
            }
            
        }
        
        //
        // GET: /Pharmacy/Edit/5
 
        public ActionResult Edit(int id)
        {
            var pharmacy = _repository.GetPharmacy(id);
            return View(pharmacy);
        }

        //
        // POST: /Pharmacy/Edit/5

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Edit(int id, FormCollection collection)
        {
            var pharmacy = _repository.GetPharmacy(id);
            try
            {
                // TODO: Add update logic here
                UpdateModel(pharmacy, collection.ToValueProvider());
                _repository.Update(pharmacy);
                    
                return RedirectToAction("Admin");
            }
            catch
            {
                return View(pharmacy);
            }
        }

        //
        // GET: /Pharmacy/Delete/5
        [AcceptVerbs(HttpVerbs.Post)] 
        //[Authorize(Roles="Administrator")]
        public ActionResult Delete(int id)
        {
            var pharmacy = _repository.GetPharmacy(id);
            _repository.Delete(pharmacy);
            _repository.Update(pharmacy);

            return RedirectToAction("Admin");
        }
    }
}
